package com.clms.rents.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.clms.rents.mapper.TabCarMapper;
import com.clms.rents.pojo.entity.TbCar;
import com.clms.rents.pojo.vo.Carvo;
import com.clms.rents.service.CarService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: clms-third
 * @description: car逻辑层
 * @author: 杨浩
 * @create: 2021-07-21 16:59
 **/


@Service
public class CarServiceimpl implements CarService{

    @Resource
    private TabCarMapper tabCarMapper;

    //查询全部，分页
    @Override
    public R selectAll(Integer currentPage, Integer pageSize, Carvo carvo) {
        Page<TbCar> page=new Page<>(currentPage,pageSize);
        IPage data=tabCarMapper.selectallcar(page,carvo);
        return R.ok().put("total",data.getTotal()).put("records",data.getRecords());
    }

    @Override
    public R insert(TbCar tbCar) {
        int i = tabCarMapper.insert(tbCar);
        if(i>0) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @Override
    public R update(TbCar tbCar) {
        int i = tabCarMapper.updateById(tbCar);
        if(i>0) {
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    //回显
    @Override
    public R selectById(Integer id) {
        TbCar tbCar = tabCarMapper.selectById(id);
        return R.ok().put("tbcar",tbCar);

    }

    @Override
    public R delete(Integer id) {
       int i=tabCarMapper.deleteById(id);
       return R.ok();
    }

    @Override
    public R treeSelect() {
        List<TbCar> list=tabCarMapper.treeSelect();
        List<Integer> y=new ArrayList<>();
        List<String> x=new ArrayList<>();
        for(TbCar tbCar:list){
            y.add(tbCar.getCount());
            x.add(tbCar.getCarName());
        }
        Map map=new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return R.ok().put("treeSelect",map);
    }
}
